package cn.iocoder.yudao.module.crossborder.inventory.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.inventory.api.dto.InventoryDTO;
import cn.iocoder.yudao.module.crossborder.inventory.api.dto.WarehouseDTO;
import cn.iocoder.yudao.module.crossborder.inventory.service.InventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 库存管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 库存管理")
@RestController
@RequestMapping("/crossborder/inventory")
@Validated
public class InventoryController {

    @Resource
    private InventoryService inventoryService;

    @GetMapping("/get")
    @Operation(summary = "获得库存记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:query')")
    public CommonResult<InventoryDTO> getInventory(@RequestParam("id") Long id) {
        InventoryDTO inventory = inventoryService.getInventory(id);
        return success(inventory);
    }

    @GetMapping("/list")
    @Operation(summary = "获得库存记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:query')")
    public CommonResult<List<InventoryDTO>> getInventoryList(@RequestParam("ids") List<Long> ids) {
        List<InventoryDTO> inventories = inventoryService.getInventoryList(ids);
        return success(inventories);
    }

    @PostMapping("/create")
    @Operation(summary = "创建库存记录")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:create')")
    public CommonResult<Long> createInventory(@RequestBody @Valid InventoryDTO inventory) {
        Long id = inventoryService.createInventory(inventory);
        return success(id);
    }

    @GetMapping("/check-stock")
    @Operation(summary = "检查库存")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "warehouseId", description = "仓库编号", required = true, example = "2048")
    @Parameter(name = "quantity", description = "数量", required = true, example = "10")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:query')")
    public CommonResult<Boolean> checkStock(@RequestParam("productId") Long productId,
                                           @RequestParam("warehouseId") Long warehouseId,
                                           @RequestParam("quantity") Integer quantity) {
        Boolean result = inventoryService.checkStock(productId, warehouseId, quantity);
        return success(result);
    }

    @PostMapping("/reserve")
    @Operation(summary = "预留库存")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "warehouseId", description = "仓库编号", required = true, example = "2048")
    @Parameter(name = "quantity", description = "数量", required = true, example = "10")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:reserve')")
    public CommonResult<Boolean> reserveStock(@RequestParam("productId") Long productId,
                                             @RequestParam("warehouseId") Long warehouseId,
                                             @RequestParam("quantity") Integer quantity) {
        Boolean result = inventoryService.reserveStock(productId, warehouseId, quantity);
        return success(result);
    }

    @PostMapping("/release")
    @Operation(summary = "释放库存")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "warehouseId", description = "仓库编号", required = true, example = "2048")
    @Parameter(name = "quantity", description = "数量", required = true, example = "10")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:release')")
    public CommonResult<Boolean> releaseStock(@RequestParam("productId") Long productId,
                                             @RequestParam("warehouseId") Long warehouseId,
                                             @RequestParam("quantity") Integer quantity) {
        Boolean result = inventoryService.releaseStock(productId, warehouseId, quantity);
        return success(result);
    }

    @GetMapping("/warehouse/get")
    @Operation(summary = "获得仓库信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:inventory:query')")
    public CommonResult<WarehouseDTO> getWarehouse(@RequestParam("id") Long id) {
        WarehouseDTO warehouse = inventoryService.getWarehouse(id);
        return success(warehouse);
    }

}
